/*
This file is part of [aconnect] library. 

Author: Artem Kustikov (kustikoff[at]tut.by)


This code is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this code.

Permission is granted to anyone to use this code for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:

1. The origin of this code must not be misrepresented; you must
not claim that you wrote the original code. If you use this
code in a product, an acknowledgment in the product documentation
would be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original code.

3. This notice may not be removed or altered from any source
distribution.
*/

#ifndef ACONNECT_CLIENT_H
#define ACONNECT_CLIENT_H

#include "types.hpp"
#include "error.hpp"
#include "util.network.hpp"

namespace aconnect 
{
	//////////////////////////////////////////////////////////////////////////
	//
	class TcpClient
	{
    public:
		TcpClient (const ip_addr_type host, const port_type port, bool autoConnect = true) throw (socket_error);
		
	private:
		TcpClient () {};
		TcpClient (const TcpClient& other) {};
		void operator=( const TcpClient& copy ) {};

	public:
		void connectToHost ()  throw (socket_error);
		
		void write (string_constptr data, size_t dataSize) throw (socket_error);
		string readToEndMark (string_constptr mark) throw (socket_error);
		string readPacket (size_t packetSize) throw (socket_error);
		void close () throw (socket_error);

		inline void setReadTimeout (const int timeoutInSeconds)	throw (socket_error);
		inline void setWriteTimeout (const int timeoutInSeconds)	throw (socket_error);

	// fields
	protected:
		ip_addr_type	_host;
		port_type		_port;
		int				_domain;

		
		socket_type _socket;
	};

	//
	//////////////////////////////////////////////////////////////////////////


	inline void TcpClient::setReadTimeout (const int timeoutInSeconds)	throw (socket_error)
	{
		util::setSocketReadTimeout (_socket, timeoutInSeconds);
	}

	inline void TcpClient::setWriteTimeout (const int timeoutInSeconds)	throw (socket_error)
	{
		util::setSocketWriteTimeout (_socket, timeoutInSeconds);
	}

}

#endif // ACONNECT_CLIENT_H
